package com.link.base.base.salecategory.service;

import com.link.base.base.product.model.Product;
import com.link.base.base.product.service.ProductService;
import com.link.base.base.salecategory.dao.mybatis.mapper.SaleCategoryMapper;
import com.link.base.base.salecategory.model.SaleCategory;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.util.LovUtil;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: unknown
 * @date: unknown
 * @Comments 客户可售品类信息
 */
@Service
public class SaleCategoryServiceImpl extends BasicServiceImpl<SaleCategory> implements SaleCategoryService {

    @Resource
    private SaleCategoryMapper saleCategoryMapper;

    @Resource
    private ProductService productService;


    @Override
    public BasicMapper<SaleCategory> getBasicMapper() {
        return saleCategoryMapper;
    }

    /**
     * @param saleCategory
     * @return SaleCategory
     * @throws Exception
     * @auth HuangLJ
     * @Comments 通过客户Id查询可售品类
     */
    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public List<SaleCategory> queryByAccntId(SaleCategory saleCategory) throws Exception {
        Long accntId = saleCategory.getAccntId();
        if (accntId == null) {
            throw new ServiceException("PUBLIC-026");
        }

        //定义返回值
        List<SaleCategory> saleCategoryListRet = new ArrayList<SaleCategory>();
        saleCategory.setPageFlag(false);
        saleCategory.setTotalFlag(true);
        List<SaleCategory> saleCategoryList = saleCategoryMapper.queryByExamplePage(saleCategory);
        for (SaleCategory saleCateLine : saleCategoryList) {
            String cateType = saleCateLine.getSaleCateType();
            if (StringUtils.equals(cateType, "Product")) {
                //如果当前为单品，则设置对应的产品信息
                String productName = saleCateLine.getProdName();
                String productCode = saleCateLine.getProdNum();
                saleCateLine.setSaleCateName(productName);
                saleCateLine.setSaleCateNum(productCode);
            } else {
                //如果是产品品牌，走产品品牌的逻辑
                //获取相应的值列表
                String value = saleCateLine.getSaleCateName();
                String code = saleCateLine.getSaleCateNum();

                //获取值列表显示值
                //获取第一级
                String name = LovUtil.getLovName(code, value);
                saleCateLine.setSaleCateName(name);

            }
            saleCategoryListRet.add(saleCateLine);
        }
        return saleCategoryListRet;
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void insert(SaleCategory saleCategory) throws Exception {
        String saleCateType = saleCategory.getSaleCateType();
        String code = saleCategory.getSaleCateNum();
        String name = saleCategory.getSaleCateName();

        //判断，如果没有传入客户id，则报错
        Long accntId = saleCategory.getAccntId();
        if (null == accntId) {
            throw new ServiceException("PUBLIC-026");
        }


        //先做逻辑处理，在插入
        //如果为产品系列或者产品品牌，则获取值列表信息存入
        String product = "Product";
        if (!product.equals(saleCateType)) {
            String value = LovUtil.getLovValue(code, name);
            saleCategory.setSaleCateName(value);
        } else {
            if (!checkProduct(saleCategory.getProductId())) {
                throw new Exception("SALESTARGET-018");
            }
        }
        //调用父类的插入方法进行插入
        super.insert(saleCategory);
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void update(SaleCategory saleCategory) throws Exception {
        String saleCateType = saleCategory.getSaleCateType();
        String code = saleCategory.getSaleCateNum();
        String name = saleCategory.getSaleCateName();

        //判断，如果没有传入客户id，则报错
        Long accntId = saleCategory.getAccntId();
        if (null == accntId) {
            throw new ServiceException("PUBLIC-026");
        }
        //先做逻辑处理，在插入
        //如果为产品系列或者产品品牌，则获取值列表信息存入
        String product = "Product";
        if (!product.equals(saleCateType)) {
            String value = LovUtil.getLovName(code, name);
            saleCategory.setSaleCateName(value);
        } else {
            if (!checkProduct(saleCategory.getProductId())) {
                throw new ServiceException("SALESTARGET-019");
            }
        }
        //调用父类的插入方法进行更新
        super.update(saleCategory);
    }

    /**
     * @param productId
     * @return boolean
     * @throws Exception
     * @auth HuangLJ
     * @校验产品是不是存在
     */
    private boolean checkProduct(Long productId) throws Exception {
        Product prod = productService.queryById(productId);
        if (prod == null) {
            return false;
        }
        return true;
    }
}
